<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html>
<head>
<title>Events in GTK#</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="GUI, GTK#, events, C#, programming, linux">
<meta name="description" content="This chapter of the GTK# tutorial covers
events.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container2">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>


<div class="content2">


<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Events in GTK#</h1>

<p>
In this part of the GTK# programming tutorial, we will talk about events.
</p>

<p>
GTK# library is an event driven system. All GUI applications react to events.
The applications start a main loop, 
which continuously checks for newly generated events. 
If there is no event, the application waits and does nothing. 
vents are generated mainly by the user of an application. But they can 
be generated by other means as well, e.g. internet connection, window manager
or a timer.
</p>

<div class="big_hor">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-9706709751191532";
/* big_horizontal */
google_ad_slot = "2904953388";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<h2>Simple event example</h2>

<p>
The next example shows, how we react to two basic events. 
</p>

<div class="codehead">quitbutton.cs</div> 
<pre class="code">
using Gtk;
using System;
 
class SharpApp : Window {

    public SharpApp() : base ("Button")
    {
        SetDefaultSize(250, 200);
        SetPosition(WindowPosition.Center);
        
        DeleteEvent += delegate { Application.Quit(); };
        
        Fixed fix = new Fixed();

        Button quit = new Button("Quit");
        quit.Clicked += OnClick;
        quit.SetSizeRequest(80, 35);

        fix.Put(quit, 50, 50);
        Add(fix);
        ShowAll();
    }
    
    void OnClick(object sender, EventArgs args)
    {
        Application.Quit();
    }

    public static void Main()
    {
        Application.Init();
        new SharpApp();
        Application.Run();
    }
}
</pre>

<p>
In our code example, we react to two events. Delete event and Clicked event. 
</p>

<p>
The delete event is triggered, when we close the window. By default, the
application does not quit, when we click on the close button in the titlebar. 
</p>

<pre class="explanation">
DeleteEvent += delegate { Application.Quit(); };
</pre>

<p>
When we use the <code>delegate</code> keyword, we can 
write in line code, that will react to this particular event. 
</p>

<pre class="explanation">
quit.Clicked += OnClick;
</pre>

<p>
Here we specify, that we react to <code>Clicked</code> event, 
with the <code>OnClick()</code> method. 
</p>

<pre class="explanation">
void OnClick(object sender, EventArgs args)
{
    Application.Quit();
}
</pre>

<p>
Here is the <code>OnClick()</code> method. It takes two parameters. 
The first parameter is the object, which triggered this event. In our case it 
is the quit button. The second parameter gives us various additional information
about the event. The event arguments depend always on the type of the event. 
The signature of each of the method can be found in the reference manual of 
the GTK# library. <a href="http://www.go-mono.com/docs/">http://www.go-mono.com/docs/</a>
</p>


<h2>Moving window</h2>

<p>
The next example shows, how we react to move events of a window. 
We show the current position of the upper left corner of our window in the titlebar. 
</p>

<div class="codehead">move.cs</div> 
<pre class="code">
using Gtk;
using System;
 
class SharpApp : Window {
 

    public SharpApp() : base("")
    {
        SetDefaultSize(250, 150);
        SetPosition(WindowPosition.Center);

        DeleteEvent += delegate { Application.Quit(); };
        Show();
    }

    protected override bool OnConfigureEvent(Gdk.EventConfigure args)
    {
        base.OnConfigureEvent(args);
        Title = args.X + ", " + args.Y;
        return true;
    }

    public static void Main()
    {
        Application.Init();
        new SharpApp();
        Application.Run();
    }
}
</pre>

<p>
In the previous example, we have plugged a delegate or a method to an event. 
In GTK#, many of the events have a handler method already. In this case, we can
override the method. This is the case, of our code example. 
</p>

<pre class="explanation">
protected override bool OnConfigureEvent(Gdk.EventConfigure args)
{
    base.OnConfigureEvent(args);
    Title = args.X + ", " + args.Y;
    return true;
}
</pre>

<p>
Here we override the predefined <code>OnConfigureEvent()</code> method. 
Configure event is triggered, when we resize or move a widget. Note, that the first
line of the method calls the default method. Without this line, the program would not
behave correctly. The next line sets the x, y coordinates of the window to the title
of the window. 
</p>

<img src="/img/gui/sharpgtk/move.png" alt="Move event">
<div class="figure">Figure: Move event</div>



<h2>EnterNotifyEvent</h2>

<p>
<code>EnterNotifyEvent</code> is emitted, when we enter the area of a widget
with a mouse pointer. 
</p>

<div class="codehead">enter.cs</div>
<pre class="code">
using Gtk;
using System;
 
class SharpApp : Window {
 

    public SharpApp() : base("Enter")
    {
        SetDefaultSize(200, 150);
        SetPosition(WindowPosition.Center);

        DeleteEvent += delegate { Application.Quit(); };

        Button button = new Button("Button");
        button.EnterNotifyEvent += OnEnter;

        Fixed fix = new Fixed();
        fix.Put(button, 20, 20);

        Add(fix);

        ShowAll();
    }


    void OnEnter(object sender, EnterNotifyEventArgs args)
    {
        Button button = (Button) sender;
        button.ModifyBg(StateType.Prelight, new Gdk.Color(220, 220, 220));
    }


    public static void Main()
    {
        Application.Init();
        new SharpApp();
        Application.Run();
    }
}
</pre>

<p>
We will change the background color of the button widget, once 
we hover a mouse pointer over it. 
</p>

<pre class="explanation">
button.EnterNotifyEvent += OnEnter;
</pre>

<p>
We plug the <code>OnEnter()</code> method to to the 
<code>EnterNotifyEvent</code>. 
</p>

<pre class="explanation">
Button button = (Button) sender;
button.ModifyBg(StateType.Prelight, new Gdk.Color(220, 220, 220));
</pre>

<p>
We get the button widget and modify the color of it's background. 
</p>


<h2>Disconnecting an event handler</h2>

<p>
We can disconnect a handler method from an event. Next code 
example demonstrates such a case. 
</p>

<div class="codehead">disconnect.cs</div>
<pre class="code">
using Gtk;
using System;
 
class SharpApp : Window {
 
    Button button;

    public SharpApp() : base("Disconnect")
    {
        SetDefaultSize(250, 150);
        SetPosition(WindowPosition.Center);

        DeleteEvent += delegate { Application.Quit(); };

        button = new Button("Button");

        CheckButton cb = new CheckButton("connect");
        cb.Toggled += OnToggled;

        Fixed fix = new Fixed();
        fix.Put(button, 30, 50);
        fix.Put(cb, 130, 50);

        Add(fix);

        ShowAll();
    }


    void OnClick(object sender, EventArgs args)
    {
        Console.WriteLine("Click");
    }

    void OnToggled(object sender, EventArgs args)
    {
        CheckButton cb = (CheckButton) sender;
        if (cb.Active) {
            button.Clicked += OnClick;
        } else {
            button.Clicked -= OnClick;
        }
    }

    public static void Main()
    {
        Application.Init();
        new SharpApp();
        Application.Run();
    }
}
</pre>

<p>
In the code example, we have a button and a check box. We show "Click" text in the
console, when we click on the button and the check box is active. 
The check box connects or disconnects a handler method from the button 
<code>Clicked</code> event. 
</p>

<pre class="explanation">
CheckButton cb = new CheckButton("connect");
cb.Toggled += OnToggled;
</pre>

<p>
We have a check box. This widget has a <code>Toggled</code> event.
We plug a <code>OnToggled()</code> method to this event.  
</p>

<pre class="explanation">
CheckButton cb = (CheckButton) sender;
if (cb.Active) {
    button.Clicked += OnClick;
} else {
    button.Clicked -= OnClick;
}
</pre>

<p>
These lines connect or disconnect an event handler, based on the
state of the check box widget. 
</p>

<img src="/img/gui/sharpgtk/disconnect.png" alt="Disconnect">
<div class="figure">Figure: Disconnect</div>


<p>
This chapter was about events in GTK#. 
</p>

<div class="center">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
<br>

<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified January 23, 2009  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

